package com.shouer.doc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.shouer.doc.R;
import com.shouer.doc.ui.main.model.MainAdvice;

/**
 * Created by YYY-Work on 2017/5/24.
 */

public class AdDialog extends Dialog implements View.OnClickListener {

    private View mDialogView;
    private TextView titleView;
    private TextView desView;

    private DialogDismissListener dialogDismissListener;

    public AdDialog(@NonNull Context context) {
        super(context);
        init(context);
    }

    public AdDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected AdDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
        getWindow().setAttributes((WindowManager.LayoutParams) params);
    }

    private void init(Context context) {

        mDialogView = View.inflate(context, R.layout.dialog_adv_layout, null);
        titleView = (TextView) mDialogView.findViewById(R.id.title);
        desView = (TextView) mDialogView.findViewById(R.id.des);
        mDialogView.findViewById(R.id.close_btn).setOnClickListener(this);
        setContentView(mDialogView);
        setCanceledOnTouchOutside(true);
    }

    public AdDialog withDialogDismissListener(DialogDismissListener dialogDismissListener) {
        this.dialogDismissListener = dialogDismissListener;
        return this;
    }

    public AdDialog withAdviceModel(MainAdvice adviceModel)
    {
        if (!TextUtils.isEmpty(adviceModel.getAdvertisementName()))
        {
            titleView.setText(adviceModel.getAdvertisementName());
        }

        if (!TextUtils.isEmpty(adviceModel.getAdvertisementDesc()))
        {
            desView.setText(adviceModel.getAdvertisementDesc().replaceAll("##","\n"));
        }
        return this;
    }


    @Override
    public void dismiss() {
        super.dismiss();
        if (null != dialogDismissListener)
        {
            dialogDismissListener.onDialogDismiss();
        }
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }
}
